package special_tree;

import code.CodeList;
import code.Opcode;
import lexer.Token;
import lexer.TokenType;
import tree.DataType;
import tree.MyTree;
import tree.Type;
import tree.TypingException;

public class LiteralTree extends MyTree {

	@Deprecated
	private int valueInt;
	@Deprecated
	private double valueDouble;
	@Deprecated
	private char valueChar;
	@Deprecated
	private Type typ;
	
	public LiteralTree(Token token) {
		super(token);
	}

	/* (non-Javadoc)
	 * @see tree.MyTree#computeType()
	 */
	@Override
	@Deprecated
	public Type computeType() throws TypingException {
		if (getToken().getType().equals(TokenType.INT_LITERAL)) {
			valueInt = Integer.parseInt(getToken().getText());
			typ = new Type(DataType.INT, "");
			return typ;
		}
		else if (getToken().getType().equals(TokenType.DOUBLE_LITERAL)) {
			valueDouble = Double.parseDouble(getToken().getText());
			typ = new Type(DataType.DOUBLE, "");
			return typ;
		}
		else if (getToken().getType().equals(TokenType.TRUE_LITERAL) || 
				getToken().getType().equals(TokenType.FALSE_LITERAL)) {
			typ = new Type(DataType.BOOL, "");
			return typ;
		}
		else if (getToken().getType().equals(TokenType.CHAR_LITERAL)) {
			valueChar = getToken().getText().charAt(0);
			typ = new Type(DataType.CHAR, "");
			return typ;
		}
		else {
			throw TypingException.illegalLiteral(getToken());
		}
	}

	/* (non-Javadoc)
	 * @see tree.MyTree#generateCode(code.CodeList)
	 */
	@Override
	@Deprecated
	public void generateCode(CodeList code) {
		if (typ.equals(new Type(DataType.INT, ""))) {
			code.generateCode(Opcode.LDIC, valueInt);
		}
		else if (typ.equals(new Type(DataType.DOUBLE, ""))) {
			code.generateCode(Opcode.LDDC, valueDouble);
		}
		else if (typ.equals(new Type(DataType.CHAR, ""))) {
			code.generateCode(Opcode.LDIC, (int) valueChar);
		}
		else {
			if (getToken().getType().equals(TokenType.FALSE_LITERAL)) {
				code.generateCode(Opcode.LDIC, 0);
			}
			if (getToken().getType().equals(TokenType.TRUE_LITERAL)) {
				code.generateCode(Opcode.LDIC, 1);
			}
		}
	}

}
